<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Reduce | Beiysd</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="manifest" href="/manifest.json">
    <link rel="apple-touch-icon" href="/assets/logo/pkp.png">
    <script>var _hmt = _hmt || [];
      (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?345ab2bf830dc5c68a96925eb1dd0b45";
        var s = document.getElementsByTagName("script")[0]; 
        s.parentNode.insertBefore(hm, s);
      })();</script>
    <meta name="description" content="Live and Study.">
    
    <link rel="preload" href="/assets/css/0.styles.bd7bd1ca.css" as="style"><link rel="preload" href="/assets/js/app.17b6b866.js" as="script"><link rel="preload" href="/assets/js/3.16fca902.js" as="script"><link rel="preload" href="/assets/js/2.aa0e2402.js" as="script"><link rel="preload" href="/assets/js/33.0841fa36.js" as="script"><link rel="prefetch" href="/assets/js/10.71654f5d.js"><link rel="prefetch" href="/assets/js/11.e3df8a38.js"><link rel="prefetch" href="/assets/js/12.f6e1589f.js"><link rel="prefetch" href="/assets/js/13.8ac48caf.js"><link rel="prefetch" href="/assets/js/14.2a93538f.js"><link rel="prefetch" href="/assets/js/15.ed5b285c.js"><link rel="prefetch" href="/assets/js/16.5e1e59d5.js"><link rel="prefetch" href="/assets/js/17.1da7896e.js"><link rel="prefetch" href="/assets/js/18.6f1b273c.js"><link rel="prefetch" href="/assets/js/19.30d92fa3.js"><link rel="prefetch" href="/assets/js/20.1068c4cc.js"><link rel="prefetch" href="/assets/js/21.f3d90412.js"><link rel="prefetch" href="/assets/js/22.a3c35868.js"><link rel="prefetch" href="/assets/js/23.3dfaf7a4.js"><link rel="prefetch" href="/assets/js/24.b7562cb8.js"><link rel="prefetch" href="/assets/js/25.94bbff07.js"><link rel="prefetch" href="/assets/js/26.9cbdf92e.js"><link rel="prefetch" href="/assets/js/27.e2189a25.js"><link rel="prefetch" href="/assets/js/28.12e893b0.js"><link rel="prefetch" href="/assets/js/29.ab1115c9.js"><link rel="prefetch" href="/assets/js/30.e2112157.js"><link rel="prefetch" href="/assets/js/31.76cdc758.js"><link rel="prefetch" href="/assets/js/32.c94896d8.js"><link rel="prefetch" href="/assets/js/34.831fd380.js"><link rel="prefetch" href="/assets/js/35.b0449875.js"><link rel="prefetch" href="/assets/js/36.30a8c074.js"><link rel="prefetch" href="/assets/js/37.1aec4dbc.js"><link rel="prefetch" href="/assets/js/38.ccd6cdf9.js"><link rel="prefetch" href="/assets/js/39.c10337d7.js"><link rel="prefetch" href="/assets/js/4.98a034f9.js"><link rel="prefetch" href="/assets/js/40.addec5bb.js"><link rel="prefetch" href="/assets/js/41.e3e62de0.js"><link rel="prefetch" href="/assets/js/42.ba7501f2.js"><link rel="prefetch" href="/assets/js/43.d8fa5ea0.js"><link rel="prefetch" href="/assets/js/44.d6dbcdb3.js"><link rel="prefetch" href="/assets/js/45.fe55507d.js"><link rel="prefetch" href="/assets/js/46.6af0f2df.js"><link rel="prefetch" href="/assets/js/47.0560a688.js"><link rel="prefetch" href="/assets/js/48.ab74b154.js"><link rel="prefetch" href="/assets/js/49.7ed2bcd8.js"><link rel="prefetch" href="/assets/js/5.2ca1c567.js"><link rel="prefetch" href="/assets/js/50.0f22d3af.js"><link rel="prefetch" href="/assets/js/51.ad770490.js"><link rel="prefetch" href="/assets/js/52.bc1bd98c.js"><link rel="prefetch" href="/assets/js/53.9ff88855.js"><link rel="prefetch" href="/assets/js/54.f2b42c40.js"><link rel="prefetch" href="/assets/js/55.3d551cd8.js"><link rel="prefetch" href="/assets/js/56.bbf7abd0.js"><link rel="prefetch" href="/assets/js/57.4b2fbc05.js"><link rel="prefetch" href="/assets/js/58.4ff6654e.js"><link rel="prefetch" href="/assets/js/59.92969875.js"><link rel="prefetch" href="/assets/js/6.f1011be9.js"><link rel="prefetch" href="/assets/js/60.4fb5be24.js"><link rel="prefetch" href="/assets/js/61.661f240d.js"><link rel="prefetch" href="/assets/js/62.d08a0fee.js"><link rel="prefetch" href="/assets/js/63.607fa9fb.js"><link rel="prefetch" href="/assets/js/64.bfa429e4.js"><link rel="prefetch" href="/assets/js/65.e184bc74.js"><link rel="prefetch" href="/assets/js/7.28ea5937.js"><link rel="prefetch" href="/assets/js/8.d804616d.js"><link rel="prefetch" href="/assets/js/9.7977e413.js">
    <link rel="stylesheet" href="/assets/css/0.styles.bd7bd1ca.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/assets/logo/pkq.png" alt="Beiysd" class="logo"> <span class="site-name can-hide">Beiysd</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="https://github.com/Beiysd" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://gitee.com/beiysd" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://blog.csdn.net/weixin_40532650" target="_blank" rel="noopener noreferrer" class="nav-link external">
  CSDN
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="https://github.com/Beiysd" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://gitee.com/beiysd" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://blog.csdn.net/weixin_40532650" target="_blank" rel="noopener noreferrer" class="nav-link external">
  CSDN
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>前端</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading"><span>CSS</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading"><span>常用方法</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading"><span>React</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading open"><span>JavaScript</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/notes/10-前端/40-JavaScript/onFocus.html" class="sidebar-link">onFocus</a></li><li><a href="/notes/10-前端/40-JavaScript/Reduce方法.html" class="active sidebar-link">Reduce方法</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/notes/10-前端/40-JavaScript/Reduce方法.html#统计字符串出现次数" class="sidebar-link">统计字符串出现次数</a></li><li class="sidebar-sub-header"><a href="/notes/10-前端/40-JavaScript/Reduce方法.html#数组去重" class="sidebar-link">数组去重</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/notes/10-前端/40-JavaScript/Reduce方法.html#单数组去重" class="sidebar-link">单数组去重</a></li><li class="sidebar-sub-header"><a href="/notes/10-前端/40-JavaScript/Reduce方法.html#数组合并去重" class="sidebar-link">数组合并去重</a></li><li class="sidebar-sub-header"><a href="/notes/10-前端/40-JavaScript/Reduce方法.html#数组对象去重" class="sidebar-link">数组对象去重</a></li></ul></li><li class="sidebar-sub-header"><a href="/notes/10-前端/40-JavaScript/Reduce方法.html#多维数组转一维" class="sidebar-link">多维数组转一维</a></li><li class="sidebar-sub-header"><a href="/notes/10-前端/40-JavaScript/Reduce方法.html#数组相同项合并" class="sidebar-link">数组相同项合并</a></li></ul></li><li><a href="/notes/10-前端/40-JavaScript/window常用方法.html" class="sidebar-link">window常用方法</a></li><li><a href="/notes/10-前端/40-JavaScript/在线读音-百度api示例.html" class="sidebar-link">百度api示例</a></li><li><a href="/notes/10-前端/40-JavaScript/对数组操作.html" class="sidebar-link">对数组操作</a></li><li><a href="/notes/10-前端/40-JavaScript/屏蔽文字选中.html" class="sidebar-link">屏蔽文字选中</a></li><li><a href="/notes/10-前端/40-JavaScript/屏蔽鼠标右键.html" class="sidebar-link">屏蔽鼠标右键</a></li><li><a href="/notes/10-前端/40-JavaScript/打包时间显示.html" class="sidebar-link">打包时间显示</a></li><li><a href="/notes/10-前端/40-JavaScript/时间小记.html" class="sidebar-link">时间小记</a></li><li><a href="/notes/10-前端/40-JavaScript/浏览器缓存.html" class="sidebar-link">浏览器缓存</a></li><li><a href="/notes/10-前端/40-JavaScript/输入框千分位数字显示.html" class="sidebar-link">输入框千分位数字显示</a></li></ul></section></li><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading"><span>Vuepress</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading"><span>Taro小程序</span> <span class="arrow right"></span></p> <!----></section></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>后端</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>随笔</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>杂谈</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>开发相关</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"><h3 class="page_title">Reduce</h3> <ul class="uls"><li title="标签" class="lis"><img src="/assets/img/tag.f21efeeb.svg" class="icons">
    </li> <li title="更新时间" class="lis"><img src="/assets/img/clock.ffb2d0be.svg" class="icons">2021-04-16
    </li> <li title="浏览人数" class="lis"><img src="/assets/img/eye.e9087eed.svg" class="icons">0
    </li></ul>  <div class="theme-default-content content__default"><h3 id="reduce"><a href="#reduce" class="header-anchor">#</a> Reduce</h3> <h3 id="定义"><a href="#定义" class="header-anchor">#</a> 定义</h3> <p><code>reduce()</code> 方法接收一个函数作为累加器，数组中的每个值（从左到右）开始缩减，最终计算为一个值。</p> <p><code>reduce()</code> 可以作为一个高阶函数，用于函数的 compose。</p> <h4 id="注意-reduce-对于空数组是不会执行回调函数的。"><a href="#注意-reduce-对于空数组是不会执行回调函数的。" class="header-anchor">#</a> 注意: reduce() 对于空数组是不会执行回调函数的。</h4> <h4 id="reduce-环境要求-google-yes-ie-9-0↑-firefox-3-0↑-opera-10-5↑"><a href="#reduce-环境要求-google-yes-ie-9-0↑-firefox-3-0↑-opera-10-5↑" class="header-anchor">#</a> reduce()环境要求 Google：Yes；IE：9.0↑； Firefox：3.0↑； opera：10.5↑</h4> <h3 id="个人理解"><a href="#个人理解" class="header-anchor">#</a> 个人理解</h3> <p>reduce 是对数组进行操作，会生成一个新数组，不会改变原数组</p> <p>标准格式：let newArr = arr.reduce((pre,cur)=&gt;{xxx},{})</p> <p>其中 newArr 为预期结果；</p> <p>arr 为目标数组；</p> <p>xxx 为逻辑函数；</p> <p>{} 为预期结果的数据类型，可以是 Array，Object，String</p> <p>原理： pre，在初始时为原型{}，也是代码最末尾那个{}标识，之后变为代码 xxx 中返回的结果，继续对 arr 中下一个元素进行操作；</p> <h2 id="统计字符串出现次数"><a href="#统计字符串出现次数" class="header-anchor">#</a> 统计字符串出现次数</h2> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">let</span> names <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">&quot;Alice&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;Bob&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;Tiff&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;Bruce&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;Alice&quot;</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> nameNum <span class="token operator">=</span> names<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">pre<span class="token punctuation">,</span> cur</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>cur <span class="token keyword">in</span> pre<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    pre<span class="token punctuation">[</span>cur<span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
    pre<span class="token punctuation">[</span>cur<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">return</span> pre<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>nameNum<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//{Alice: 2, Bob: 1, Tiff: 1, Bruce: 1}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><h2 id="数组去重"><a href="#数组去重" class="header-anchor">#</a> 数组去重</h2> <h3 id="单数组去重"><a href="#单数组去重" class="header-anchor">#</a> 单数组去重</h3> <div class="language-js line-numbers-mode"><pre class="language-js"><code>bbb <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Set</span><span class="token punctuation">(</span>aaa<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="数组合并去重"><a href="#数组合并去重" class="header-anchor">#</a> 数组合并去重</h3> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function-variable function">distinct</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">a<span class="token punctuation">,</span> b</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> Array<span class="token punctuation">.</span><span class="token function">from</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Set</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token operator">...</span>a<span class="token punctuation">,</span> <span class="token operator">...</span>b<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="数组对象去重"><a href="#数组对象去重" class="header-anchor">#</a> 数组对象去重</h3> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">let</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span>
  <span class="token punctuation">{</span> code<span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span> name<span class="token operator">:</span> <span class="token string">&quot;11&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token punctuation">{</span> code<span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span> name<span class="token operator">:</span> <span class="token string">&quot;22&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token punctuation">{</span> code<span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span> name<span class="token operator">:</span> <span class="token string">&quot;33&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> newArr <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">pre<span class="token punctuation">,</span> cur</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token comment">//true:重复;false:不重复</span>
  <span class="token keyword">let</span> indx <span class="token operator">=</span> pre<span class="token punctuation">.</span><span class="token function">findIndex</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">v</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> v<span class="token punctuation">.</span>code <span class="token operator">===</span> cur<span class="token punctuation">.</span>code<span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>indx<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> pre<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span>cur<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> pre<span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>newArr<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// [{ code: 1, name: &quot;11&quot; },{ code: 2, name: &quot;33&quot; }]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><h2 id="多维数组转一维"><a href="#多维数组转一维" class="header-anchor">#</a> 多维数组转一维</h2> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">let</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span>
  <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
  <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
  <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token function-variable function">newArr</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">arr</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> arr<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span>
    <span class="token punctuation">(</span><span class="token parameter">pre<span class="token punctuation">,</span> cur</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> pre<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span>Array<span class="token punctuation">.</span><span class="token function">isArray</span><span class="token punctuation">(</span>cur<span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token function">newArr</span><span class="token punctuation">(</span>cur<span class="token punctuation">)</span> <span class="token operator">:</span> cur<span class="token punctuation">)</span><span class="token punctuation">,</span>
    <span class="token punctuation">[</span><span class="token punctuation">]</span>
  <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">newArr</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//[0, 1, 2, 3, 4, 5, 6, 7]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h2 id="数组相同项合并"><a href="#数组相同项合并" class="header-anchor">#</a> 数组相同项合并</h2> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">let</span> names <span class="token operator">=</span> <span class="token punctuation">[</span>
  <span class="token punctuation">{</span> name<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">&quot;Alice&quot;</span><span class="token punctuation">]</span><span class="token punctuation">,</span> code<span class="token operator">:</span> <span class="token string">&quot;101&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token punctuation">{</span> name<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">&quot;Bob&quot;</span><span class="token punctuation">]</span><span class="token punctuation">,</span> code<span class="token operator">:</span> <span class="token string">&quot;102&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token punctuation">{</span> name<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">&quot;Alice&quot;</span><span class="token punctuation">]</span><span class="token punctuation">,</span> code<span class="token operator">:</span> <span class="token string">&quot;101&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token punctuation">{</span> name<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">&quot;Tiff&quot;</span><span class="token punctuation">]</span><span class="token punctuation">,</span> code<span class="token operator">:</span> <span class="token string">&quot;101&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token punctuation">{</span> name<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">&quot;Bruce&quot;</span><span class="token punctuation">]</span><span class="token punctuation">,</span> code<span class="token operator">:</span> <span class="token string">&quot;105&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> arrMerge <span class="token operator">=</span> names<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">pre<span class="token punctuation">,</span> curr</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> eqIndex <span class="token operator">=</span> pre<span class="token punctuation">.</span><span class="token function">findIndex</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">item</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> item<span class="token punctuation">.</span>code <span class="token operator">===</span> curr<span class="token punctuation">.</span>code<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>eqIndex <span class="token operator">!==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    pre<span class="token punctuation">[</span>eqIndex<span class="token punctuation">]</span><span class="token punctuation">.</span>name<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token operator">...</span>curr<span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> pre<span class="token punctuation">;</span>
  <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> pre<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span>curr<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arrMerge<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//[{code:'101',name:['Alice','Alice','Tiff']}, {code:'102',name:['Bob']}, {code:'105',name:['Bruce']}]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div></div> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/notes/10-前端/40-JavaScript/onFocus.html" class="prev">
        onFocus
      </a></span> <span class="next"><a href="/notes/10-前端/40-JavaScript/window常用方法.html">
        window常用方法
      </a>
      →
    </span></p></div>  <button>click</button> <div class="back_top_content" data-v-26f75736><!----></div></main></div><div class="global-ui"><MySWUpdatePopup></MySWUpdatePopup></div></div>
    <script src="/assets/js/app.17b6b866.js" defer></script><script src="/assets/js/3.16fca902.js" defer></script><script src="/assets/js/2.aa0e2402.js" defer></script><script src="/assets/js/33.0841fa36.js" defer></script>
  </body>
</html>
